10308. Roads in the North

 

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are built in such a way that there is only one route from a village to a village that does not pass through some other village twice. Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area. The area has up to 10.000 villages connected by road segments. The villages are numbered from 1.

 

Input. Contains several sets. Each input set is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way. Two consecutive sets are separated by a blank line.

 

Output. For each set of input, you are to output a single line containing a single integer: the road distance between the two most remote villages in the area.

 

Sample input

Sample output

5 1 6

1 4 5

6 3 9

2 6 8

6 1 7

22

 

 

ÐÅØÅÍÈÅ

äèàìåòð äåðåâà

 

Àíàëèç àëãîðèòìà

Çàäàíî âçâåøåííîå äåðåâî. Íåîáõîäèìî íàéòè ðàññòîÿíèå ìåæäó äâóìÿ ñàìûìè äàëüíèìè âåðøèíàìè (äèàìåòð äåðåâà).

 

Ïðèìåð

Ãðàô, ïðèâåäåííûé â ïðèìåðå, èìååò âèä:

 

Ðåàëèçàöèÿ àëãîðèòìà

 

#include <cstdio>

#include <vector>

#include <algorithm>

#define MAX 10010

using namespace std;

 

vector<vector<pair<int, int> > > g;

int diameter, i, n, u, v, dist;

char s[MAX];

 

int dfs(int v, int prev = -1)

{

  int h1 = 0, h2 = 0; // highest and second highest height

  for (int i = 0; i < g[v].size(); i++)

  {

    int to = g[v][i].first;

    int len = g[v][i].second;

    if (to != prev)

    {

      int h = dfs(to, v) + len;

      if (h > h1) h2 = h1, h1 = h;

      else if (h > h2) h2 = h;

    }

    diameter = max(diameter, h1 + h2);

  }

  return h1;

}

 

int main(void)

{

  g.clear();

  g.resize(MAX);

  while (gets(s))

  {

    if (s[0] != 0)

    {

      sscanf(s, "%d %d %d", &u, &v, &dist);

      g[u].push_back(make_pair(v, dist));

      g[v].push_back(make_pair(u, dist));

    }

    else

    {

      diameter = 0;

      dfs(1);

      printf("%d\n", diameter);

      g.clear();

      g.resize(MAX);

    }

  }

  diameter = 0;

  dfs(1);

  printf("%d\n", diameter);

  return 0;

}

 

Ðåàëèçàöèÿ àëãîðèòìà ïîèñê â øèðèíó

 

#include <cstdio>

#include <iostream>

#include <deque>

#include <vector>

#include <string>

#include <algorithm>

#define MAX 10001

using namespace std;

 

int i, j, n;

int b, e, tests;

long long dist;

string s;

 

struct road

{

  int v;

  long long dist;

  road(int v, long long dist) : v(v), dist(dist) {}

};

 

vector<vector<road> > g;

vector<long long> d;

 

int bfs(int v)

{

  d = vector<long long>(MAX, -1);

  d[v] = 0;

 

  deque<int> q;

  q.push_back(v);

  while (!q.empty())

  {

    int v = q.front(); q.pop_front();

    for (int i = 0; i < g[v].size(); i++)

    {

      int to = g[v][i].v;

      if (d[to] == -1)

      {

        d[to] = d[v] + g[v][i].dist;

        q.push_back(to);

      }

    }

  }

  return max_element(d.begin() + 1, d.begin() + MAX) - d.begin();

}

 

int main(void)

{

  g.resize(MAX);

 

  while (getline(cin, s))

  {

    if (s != "")

    {

      sscanf(s.c_str(), "%d %d %lld", &b, &e, &dist);

      g[b].push_back(road(e, dist));

      g[e].push_back(road(b, dist));

    }

    else

    {

      int v = bfs(1);

      v = bfs(v);

      printf("%lld\n", d[v]);

      g.clear();

      g.resize(MAX);

    }

  }

 

  int v = bfs(1);

  v = bfs(v);

  printf("%lld\n", d[v]);

  return 0;

}